package cn.wuxing.mediation.web.controller.common;

import cn.hutool.core.io.FileUtil;
import cn.wuxing.mediation.businessplatform.mapper.business.CmEventApplicantMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmNotDisputeEventMapper;
import cn.wuxing.mediation.businessplatform.mapper.system.AreaMapper;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.infoconfig.mapper.IcWindowMapper;
import cn.wuxing.mediation.system.domain.NotDisputeEvent;
import cn.wuxing.mediation.web.imports.domain.EventApplicantExcel;
import cn.wuxing.mediation.web.imports.domain.ImportNonDisputeEvent;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@Slf4j
@Service
public class ImportDataHandler {
    @Autowired
    private CmNotDisputeEventMapper notDisputeEventMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CmEventApplicantMapper eventApplicantMapper;
    @Autowired
    private IcWindowMapper windowMapper;

    public AjaxResult importNonDisputeEvent(File file) {
        if (file == null) {
            return AjaxResult.error("请选择文件");
        }

        try {
            EasyExcel.read(FileUtil.getInputStream(file),
                    ImportNonDisputeEvent.class, new NonDisputeEventListener(notDisputeEventMapper, areaMapper, windowMapper)).sheet().doRead();
        } catch (Exception e) {
            log.error("【导入】- 非纠纷事件 ：导入失败。", e);
            throw new RuntimeException(e);
        }
        return AjaxResult.success("导入成功");
    }

    public AjaxResult importEventApplicant(File file) {
        if (file == null) {
            return AjaxResult.error("请选择文件");
        }

        try {
            EasyExcel.read(FileUtil.getInputStream(file),
                    EventApplicantExcel.class, new EventApplicantListener(eventApplicantMapper, notDisputeEventMapper)).sheet().doRead();
        } catch (Exception e) {
            log.error("【导入】- 事件申请人：导入失败。", e);
            throw new RuntimeException(e);
        }
        return AjaxResult.success("导入成功");
    }
}
